var SiteNote; if (!SiteNote) SiteNote = {};

if (!SiteNote.ScriptLogLevel) {
    SiteNote.ScriptLogLevel = {None: 0, Error: 1, Info: 2, Debug: 3};
}

SiteNote.DebugWriter = function()
{
    var debugWindow;
    this.level = SiteNote.ScriptLogLevel.None;

    this.setLevel = function(level)
    {
        switch(level.toLowerCase())
        {
            case "none":
                this.level = SiteNote.ScriptLogLevel.None;
                break;
            case "error":
                this.level = SiteNote.ScriptLogLevel.Error;
                break;
            case "info":
                this.level = SiteNote.ScriptLogLevel.Info;
                break;
            case "debug":
                this.level = SiteNote.ScriptLogLevel.Debug;
                break;
            default:
                throw "Unexpected log level: " + level;
        }
    };


    function createDebugWindow()
    {
        try
        {
            debugWindow = document.createElement("div");
            var div = debugWindow;
            div.style.fontSize = "11px";
            div.style.fontFamily = "Tahoma";
            div.style.position = "fixed";
            div.style.width = "400px";
            div.style.height = "100px";
            div.style.overflow = "auto";
            div.style.border = "solid 1px black";
            div.style.backgroundColor = "white";
            div.style.color = "black";
            div.style.bottom = "0px";
            div.style.right = "0px";
            div.style.zIndex = "1000";
            div.style.textAlign = "left";
            div.title = "Click to copy to clipboard";
            div.setAttribute("id", "SiteNote_DebugWindow");
            div.onselectstart = function()
            {
                event.cancelBubble = true;
                return true;
            };

            div.onclick = function()
            {
                window.clipboardData.setData("Text", debugWindow.innerHTML);
                alert("The log is copied to clipboard");
            };
            document.body.appendChild(debugWindow);
        }
        catch(e) {
            alert(e);
        }
    };


    this.writeln = function(str, bgColor)
    {
        if (!debugWindow)
        {
            createDebugWindow();
        }

        var dw = debugWindow;

        var msgBlock = document.createElement("div");
        if (bgColor)
        {
            msgBlock.style.backgroundColor = bgColor;
        }
        msgBlock.innerHTML = str.replace(' ', '&nbsp;');
        dw.appendChild(msgBlock);
        if (dw.scrollHeight > dw.scrollTop)
        {
            dw.scrollTop = dw.scrollHeight;
        };
    };

    this.info = function(str)
    {
        if (this.level >= SiteNote.ScriptLogLevel.Info) this.writeln(str, "#30B346");
    };

    this.error = function(str)
    {
        if (this.level >= SiteNote.ScriptLogLevel.Error) this.writeln(str, "#F01717");
    };

    this.debug = function(str)
    {
        if (this.level >= SiteNote.ScriptLogLevel.Debug) this.writeln(str, "#3030B3");
    };
};

SiteNote.Debug = new SiteNote.DebugWriter();

SiteNote.attachEvent = function(obj, evt, fnc, useCapture) {
    if (!useCapture) useCapture = false;

    if (obj.addEventListener) {
        obj.addEventListener(evt, fnc, useCapture);
        return true;
    }
    else if (obj.attachEvent) return obj.attachEvent("on" + evt, fnc);
    else {
        if (!obj.myEvents) obj.myEvents={};
        if (!obj.myEvents[evt]) obj.myEvents[evt] = [];
        var evts = obj.myEvents[evt];
        evts[evts.length] = fnc;
        obj['on' + evt] = function() {
            if (!obj || !obj.myEvents || !obj.myEvents[evt]) return;
            var evts = obj.myEvents[evt];
            for (var i=0, len=evts.length; i < len; i++) evts[i]();
        };
    }
};

function fixWebPartsManager() {
    SiteNote.attachEvent(window, 'load', function() {
        if (__wpm) {
            var prevDragDropHandler = __wpm.ContinueWebPartDragDrop;
            __wpm.ContinueWebPartDragDrop = function() {
                prevDragDropHandler();
                var dragState = __wpm.dragState;
                if (dragState != null)
                {
                    var height = $(window).height();
                    if ((height < event.clientY) || event.clientY < 0) {
                        var step = 0;
                        if (event.clientY < 0)
                        {
                            step = event.clientY * 2;
                        }
                        else
                        {
                            step = (event.clientY - height) * 2;
                        }
                        var scrollPos = $('html,body').attr('scrollTop') + step;
                        $('html,body').attr('scrollTop', scrollPos);
                    }
                }
            };
        }
    });
};

SiteNote.MessageBox = {};

/**
 * Shows confirmation dialog box with the specified text and icon.
 * @param text Message text
 * @param type Type of the message. Can have the following values: "question", "info",  "alert".
 */
SiteNote.MessageBox.confirm = function(text, type) {
    var args = {};
    args.message = text;
    args.type = type;
    var features = "dialogHeight:150px;dialogWidth:500px;status:no;scroll:no;resizable:no;help:no;";
    return window.showModalDialog(SiteNote.Settings.AppPath + SiteNote.Settings.VirtualPath + "/XmlEditor/Confirm.aspx", args,
            features);
}

if (!SiteNote.Utiles) SiteNote.Utils = new function() {
    var g_oHtmlEncodeElement;

    this.htmlEscape = function(text)
    {
        g_oHtmlEncodeElement = g_oHtmlEncodeElement || document.createElement("div");
        g_oHtmlEncodeElement.innerText = g_oHtmlEncodeElement.textContent = text;
        return g_oHtmlEncodeElement.innerHTML;
    };

    this.htmlUnescape = function(html)
    {
        g_oHtmlEncodeElement = g_oHtmlEncodeElement || document.createElement("div");
        g_oHtmlEncodeElement.textContent = g_oHtmlEncodeElement.innerText = "";
        g_oHtmlEncodeElement.innerHTML = html;
        return g_oHtmlEncodeElement.innerText || g_oHtmlEncodeElement.textContent;
    };
};
